package com.zhuangjie.shop.controller;

import com.zhaungjie.base.conduit.R;
import com.zhuangjie.mvc.validator.valid.DefaultGroup;
import com.zhuangjie.security.annotations.NoAuthentication;
import com.zhuangjie.security.annotations.RequiresRoles;
import com.zhuangjie.shop.dto.activity.FullReductionActivityDTO;
import com.zhuangjie.shop.dto.activity.NewCustomerActivityDTO;
import com.zhuangjie.shop.dto.activity.ReduceDeliveryFreeActivityDTO;
import com.zhuangjie.shop.entity.ShopActivityInstance;
import com.zhuangjie.shop.enums.ShopRole;
import com.zhuangjie.shop.service.ShopActivityInstanceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhuangjie
 * @since 2024-03-12
 */
@RestController
@RequestMapping("/shop-activity-instance")
@RequiresRoles(ShopRole.admin)
public class ShopActivityInstanceController {
    @Autowired
    private ShopActivityInstanceService shopActivityInstanceService;


    @GetMapping("/{activityTypeId}")
    @ApiOperation("获取指定活动类型的店铺活动列表")
    public R getShopActivityList(@PathVariable("activityTypeId") Long activityTypeId) {
        return R.require(List.class,shopActivityInstanceService.getShopActivityListByType(activityTypeId));
    }

    @GetMapping("/activity/{activityInstanceId}")
    @ApiOperation("获取指定活动实例的信息")
    public R getShopActivityInstance(@PathVariable("activityInstanceId") Long activityInstanceId) {
        return R.require(ShopActivityInstance.class,shopActivityInstanceService.getShopActivityInstanceById(activityInstanceId));
    }

    @DeleteMapping("/activity/{activityInstanceId}")
    @ApiOperation("删除指定活动实例信息")
    public R deleteShopActivityInstance(@PathVariable("activityInstanceId") Long activityInstanceId) {
        return R.capture(()->shopActivityInstanceService.removeShopActivityInstanceById(activityInstanceId));
    }

    @PutMapping("/full-reduction-activity")
    @ApiOperation("编辑或添加满减活动")
    public R putFullReductionActivity(@Validated({DefaultGroup.class}) @RequestBody FullReductionActivityDTO fullReductionActivityDTO) {
        return R.capture(()->shopActivityInstanceService.putFullReductionActivity(fullReductionActivityDTO));
    }

    @PutMapping("/new-customers-activity")
    @ApiOperation("编辑或添加新客立减活动")
    public R putNewCustomerActivity(@Validated({DefaultGroup.class}) @RequestBody NewCustomerActivityDTO fullReductionActivityDTO) {
        return R.capture(()->shopActivityInstanceService.putNewCustomerActivity(fullReductionActivityDTO));
    }

    @PutMapping("/reduce-delivery-free-activity")
    @ApiOperation("减免配送费活动")
    public R putReduceDeliveryFreeActivity(@Validated({DefaultGroup.class}) @RequestBody ReduceDeliveryFreeActivityDTO reduceDeliveryFreeActivityDTO) {
        return R.capture(()->shopActivityInstanceService.putReduceDeliveryFreeActivity(reduceDeliveryFreeActivityDTO));
    }

    @PutMapping("/divine-voucher/{state}")
    @ApiOperation("参与与退出神券活动")
    public R changeDivineVoucherStatus(@PathVariable("state") Boolean state) {
        return R.capture(()->shopActivityInstanceService.participateOrExitDivineVoucher(state));
    }

    @ApiOperation("店铺是否支持神券活动")
    @GetMapping("/is-super-divine-voucher/{shopId}")
    @NoAuthentication
    public R isShopInvolvedInPlatformCouponActivity(@PathVariable("shopId") Long shopId) {
        return R.require(Boolean.class,shopActivityInstanceService.isShopInvolvedInPlatformCouponActivity(shopId));
    }

}
